#include "myFlash.h"

static FLASH_EraseInitTypeDef EraseInitStruct = {
    .TypeErase = FLASH_TYPEERASE_PAGES,
    .PageAddress = FLASH_START_ADDR,
    .NbPages = 1,
};


bool myFlash_write(uint32_t addr, const uint32_t *data, uint16_t len)
{
    HAL_FLASH_Unlock();
    uint32_t SectorError = 0;
    __disable_irq();
    if(HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError)==HAL_ERROR)
    {
        return false;
    }
    for(uint16_t i=0; i<len; i++)
    {
        if(HAL_FLASH_Program(TYPEPROGRAM_WORD, addr, data[i])==HAL_ERROR)
        {
            return false;
        }
        addr+=4;
    }
    HAL_FLASH_Lock();
    __enable_irq();
    return true;
}

uint32_t myFlash_read(uint32_t addr)
{
    return *(uint32_t*)addr;
}





